﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using LunchPad.Entity;
using System.Data;


namespace LunchPad.Business
{
    public class UserBC
    {
        public static List <User> GetList()
        {
            using (IDataReader reader = DatabaseFactory.CreateDatabase("Lunchpad").ExecuteReader("UserGetList"))
            {
                return FillObjectCollection(reader);
            }
        }
        private static User FillObject(IDataReader reader)
        {
            User User = new User();
            User.UserId = Convert.ToInt32(reader["User_ID"]);
            User.NameFirst = Convert.ToString(reader["Name_First"]);
            User.NameLast = Convert.ToString(reader["Name_Last"]);
            User.CarrierId = Convert.ToInt32(reader["Carrier_ID"]);
            User.PhoneNumber = Convert.ToString(reader["Phone_Number"]);
            User.UserName = Convert.ToString(reader["Phone_Number"]);
            User.Password = Convert.ToString(reader["Password"]);
            User.DefaultLat = Convert.ToDecimal(reader["Default_Lat"]);
            User.DefaultLong = Convert.ToDecimal(reader["Default_Long"]);
            User.CurrentLat = Convert.ToDecimal(reader["Current_Lat"]);
            User.CurrentLong = Convert.ToDecimal(reader["Current_Long"]);
            User.Email = Convert.ToString(reader["Email"]);
            User.SMS = Convert.ToString(reader["SMS"]);
            User.UserEmail = Convert.ToString(reader["UserEmail"]);
            return User;
        }



        private static List<User> FillObjectCollection(IDataReader reader)
        {
            List<User> Users = new List<User>();

            while (reader.Read())
            {
                User User = FillObject(reader);

                Users.Add(User);
            }

            return Users;
        }

        public static User Get(int UserID)
        {
            User User = null;
            using (IDataReader reader = DatabaseFactory.CreateDatabase("Lunchpad").ExecuteReader("UserGet",UserID))
            {
                if (reader.Read())
                {
                    User = FillObject(reader);
                }
            }
            return User;
        }

        public static User Update(User User)
        {
            try
            {
                DatabaseFactory.CreateDatabase("Lunchpad").ExecuteNonQuery(
                    "UserUpdate"
                    ,User.UserId
                    ,User.NameFirst
                    ,User.NameLast
                    ,User.Email
                    ,User.SMS
                    ,User.UserEmail
                    );

                return Get(User.UserId);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error: UserUpdate", ex);
            }
        }


    }
}
